package com.delivery.controller.admin;

import com.delivery.dto.SetmealDTO;
import com.delivery.dto.SetmealPageQueryDTO;
import com.delivery.result.PageResult;
import com.delivery.result.Result;
import com.delivery.service.SetmealService;
import com.delivery.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    SetmealService setmealService;

    /**
     * 添加套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmeal", key = "#setmealDTO.categoryId")
    public Result addCombo(@RequestBody SetmealDTO setmealDTO){
        setmealService.addCombo(setmealDTO);
        return Result.success();
    }

    /**
     * 根据套餐ID查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetmealVO> getComboById(@PathVariable Long id){
        SetmealVO setmealVO = setmealService.getComboById(id);
        return Result.success(setmealVO);
    }

    /**
     * 分页查询套餐
     * @param dto
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO dto){
        PageResult pageResult = setmealService.page(dto);
        return Result.success(pageResult);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 起售停售套餐
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public Result startStopCombo(@PathVariable Integer status, Long id){
        setmealService.startStopCombo(status,id);
        return Result.success();
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public Result batchDeleteCombo(@RequestParam("ids") List<Long> ids){
        setmealService.batchDeleteCombo(ids);
        return Result.success();
    }
}
